<template>
  <div class="card" @click="handleCardClick" @contextmenu.prevent="handleRightClick">
    <!-- 预览区域 -->
    <div class="preview">
      <img 
        v-if="previewUrl" 
        :src="previewUrl" 
        :alt="title"
        class="preview-image"
        @error="handleImageError"
      />
      <div v-else class="preview-placeholder">
        <div class="file-icon">📄</div>
      </div>
      
      <!-- 下载按钮 -->
      <div class="download-overlay">
        <button class="download-btn" @click.stop="handleDownload">
          <span class="download-icon">⬇️</span>
          下载
        </button>
      </div>
    </div>
    
    <!-- 文件信息 -->
    <div class="file-info">
      <div class="title">{{ title }}</div>
      <div class="file-meta">
        <span class="file-size">{{ fileSize }}</span>
        <span v-if="fileType" class="file-type">{{ fileType }}</span>
      </div>
      <div v-if="description" class="description">{{ description }}</div>
      <div v-if="tags && tags.length > 0" class="tags">
        <span v-for="tag in tags" :key="tag" class="tag">{{ tag }}</span>
      </div>
    </div>
    
    <!-- 右键菜单 -->
    <div 
      v-if="showContextMenu" 
      class="context-menu" 
      :style="{ left: contextMenuX + 'px', top: contextMenuY + 'px' }"
      @click.stop
    >
      <div class="context-menu-item" @click="handleDownload">
        <span class="menu-icon">⬇️</span>
        <span>下载</span>
      </div>
      <div class="context-menu-item delete-item" @click="handleDelete">
        <span class="menu-icon">🗑️</span>
        <span>删除</span>
      </div>
    </div>
    
    <!-- 删除确认对话框 -->
    <div v-if="showDeleteConfirm" class="delete-confirm-overlay" @click="cancelDelete">
      <div class="delete-confirm-dialog" @click.stop>
        <div class="confirm-title">确认删除</div>
        <div class="confirm-message">确定要删除文件 "{{ title }}" 吗？此操作无法撤销。</div>
        <div class="confirm-buttons">
          <button class="confirm-btn cancel-btn" @click="cancelDelete">取消</button>
          <button class="confirm-btn delete-btn" @click="confirmDelete">删除</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    fileData: {
      type: Object,
      default: () => ({})
    },
    previewUrl: {
      type: String,
      default: null
    },
    fileSize: {
      type: String,
      default: ''
    },
    fileType: {
      type: String,
      default: ''
    },
    tags: {
      type: Array,
      default: () => []
    },
    description: {
      type: String,
      default: ''
    }
  },
  emits: ['download', 'delete'],
  data() {
    return {
      showContextMenu: false,
      contextMenuX: 0,
      contextMenuY: 0,
      showDeleteConfirm: false
    }
  },
  mounted() {
    // 监听全局点击事件来隐藏右键菜单
    document.addEventListener('click', this.hideContextMenu)
  },
  beforeUnmount() {
    // 清理事件监听器
    document.removeEventListener('click', this.hideContextMenu)
  },
  methods: {
    handleDownload() {
      this.$emit('download')
    },
    handleCardClick() {
      // 点击卡片也可以下载
      this.handleDownload()
    },
    handleImageError(event) {
      // 图片加载失败时隐藏图片
      event.target.style.display = 'none'
    },
    handleRightClick(event) {
      console.log('右键点击事件触发')
      // 显示右键菜单
      this.showContextMenu = true
      
      // 计算菜单位置，确保不超出视窗边界
      const rect = this.$el.getBoundingClientRect()
      const menuWidth = 120 // 预估菜单宽度
      const menuHeight = 80 // 预估菜单高度
      
      let x = event.clientX - rect.left
      let y = event.clientY - rect.top
      
      // 防止菜单超出卡片右边界
      if (x + menuWidth > rect.width) {
        x = rect.width - menuWidth
      }
      
      // 防止菜单超出卡片下边界
      if (y + menuHeight > rect.height) {
        y = rect.height - menuHeight
      }
      
      this.contextMenuX = Math.max(0, x)
      this.contextMenuY = Math.max(0, y)
    },
    hideContextMenu() {
      this.showContextMenu = false
    },
    handleDelete() {
      this.hideContextMenu()
      this.showDeleteConfirm = true
    },
    confirmDelete() {
      this.showDeleteConfirm = false
      this.$emit('delete', this.fileData)
    },
    cancelDelete() {
      this.showDeleteConfirm = false
    }
  }
}
</script>

<style scoped>
.card {
  width: 335px;
  height: 280px;
  border-radius: 18px;
  background: linear-gradient(145deg, #ffffff, #f0f0f0);
  border: 1px solid rgba(161, 161, 161, 0.3);
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  
  /* 多层阴影创造立体效果 */
  box-shadow: 
    /* 主要阴影 */
    0 8px 32px rgba(0, 0, 0, 0.02),
    /* 近距离阴影 */
    0 2px 8px rgba(0, 0, 0, 0.08),
    /* 内部高光 */
    inset 0 1px 0 rgba(255, 255, 255, 0.8),
    /* 边缘高光 */
    inset 0 0 0 1px rgba(255, 255, 255, 0.4);
}

/* 添加高光效果的伪元素 */
.card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 50%;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.6) 0%,
    rgba(255, 255, 255, 0.2) 50%,
    transparent 100%
  );
  border-radius: 18px 18px 0 0;
  pointer-events: none;
  z-index: 0;
}

/* 悬停效果增强立体感 */
.card:hover {
  transform: translateY(-4px);
  box-shadow: 
    /* 悬停时更强的阴影 */
    0 16px 48px rgba(0, 0, 0, 0.18),
    0 4px 16px rgba(0, 0, 0, 0.12),
    /* 内部高光保持 */
    inset 0 1px 0 rgba(255, 255, 255, 0.9),
    inset 0 0 0 1px rgba(255, 255, 255, 0.5);
}

.card:hover .download-overlay {
  opacity: 1;
}

/* 预览区域 */
.preview {
  width: calc(100% - 12px);
  height: 180px;
  margin: 6px;
  border-radius: 14px;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 14px;
}

.preview-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.file-icon {
  font-size: 48px;
  opacity: 0.7;
}

/* 下载按钮覆盖层 */
.download-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
  border-radius: 14px;
}

.download-btn {
  background: rgba(255, 255, 255, 0.9);
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  color: #333;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.download-btn:hover {
  background: rgba(255, 255, 255, 1);
  transform: scale(1.05);
}

.download-icon {
  font-size: 16px;
}

/* 文件信息区域 */
.file-info {
  padding: 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: relative;
  z-index: 1;
}

.title {
  color: #333;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.2;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-meta {
  display: flex;
  gap: 8px;
  font-size: 12px;
  color: #666;
}

.file-size {
  background: #e3f2fd;
  color: #1976d2;
  padding: 2px 6px;
  border-radius: 4px;
}

.file-type {
  background: #f3e5f5;
  color: #7b1fa2;
  padding: 2px 6px;
  border-radius: 4px;
}

.description {
  font-size: 12px;
  color: #666;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
}

.tag {
  background: #007bff;
  color: white;
  padding: 2px 6px;
  border-radius: 8px;
  font-size: 10px;
  font-weight: 500;
}

/* 右键菜单样式 */
.context-menu {
  position: absolute;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.12),
    0 2px 8px rgba(0, 0, 0, 0.08);
  z-index: 1000;
  min-width: 120px;
  overflow: hidden;
  animation: contextMenuFadeIn 0.15s ease-out;
}

@keyframes contextMenuFadeIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(-5px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.context-menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: all 0.2s ease;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.context-menu-item:last-child {
  border-bottom: none;
}

.context-menu-item:hover {
  background: rgba(0, 123, 255, 0.1);
  color: #007bff;
}

.context-menu-item.delete-item:hover {
  background: rgba(220, 53, 69, 0.1);
  color: #dc3545;
}

.menu-icon {
  font-size: 16px;
  width: 16px;
  text-align: center;
}

/* 删除确认对话框样式 */
.delete-confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  animation: overlayFadeIn 0.2s ease-out;
}

@keyframes overlayFadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.delete-confirm-dialog {
  background: white;
  border-radius: 12px;
  padding: 24px;
  max-width: 400px;
  width: 90%;
  box-shadow: 
    0 20px 60px rgba(0, 0, 0, 0.2),
    0 8px 24px rgba(0, 0, 0, 0.1);
  animation: dialogSlideIn 0.3s ease-out;
}

@keyframes dialogSlideIn {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.confirm-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
}

.confirm-message {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  margin-bottom: 20px;
}

.confirm-buttons {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.confirm-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 70px;
}

.cancel-btn {
  background: #f8f9fa;
  color: #6c757d;
  border: 1px solid #dee2e6;
}

.cancel-btn:hover {
  background: #e9ecef;
  color: #495057;
}

.delete-btn {
  background: #dc3545;
  color: white;
}

.delete-btn:hover {
  background: #c82333;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(220, 53, 69, 0.3);
}
</style>